package atm;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ChangePanel extends JPanel implements ActionListener {
    private ATM atm;
    private JButton sureBtn;
    private JButton clearBtn;
    private Icon sureImg = Util.getImageIcon("/images/buttonSure.jpg");
    private Icon clearImg = Util.getImageIcon("/images/buttonClear.jpg");

    private JTextField old=new JTextField();
    private JTextField new1=new JTextField();
    private JTextField new2=new JTextField();

    public JTextField getOld() {
        return old;
    }

    public void setOld(JTextField old) {
        this.old = old;
    }

    public JTextField getNew1() {
        return new1;
    }

    public void setNew1(JTextField new1) {
        this.new1 = new1;
    }

    public JTextField getNew2() {
        return new2;
    }

    public void setNew2(JTextField new2) {
        this.new2 = new2;
    }

    public ChangePanel(ATM atm){
        this.atm = atm;
        this.setLayout(null);
        this.setOpaque(false);

        sureBtn = new JButton(sureImg);
        sureBtn.setBounds(10, 40, 140, 50);
        this.add(sureBtn);
        sureBtn.addActionListener(this);

        clearBtn = new JButton(clearImg);
        clearBtn.setBounds(10, 100, 140, 50);
        this.add(clearBtn);
        clearBtn.addActionListener(this);

        JLabel jiu=new JLabel("请输入旧密码");
        jiu.setBounds(400, 220, 90, 20);
        this.add(jiu);
        old.setBounds(510,220,90,20);
        this.add(old);

        JLabel xin=new JLabel("请输入新密码");
        xin.setBounds(400, 250, 90, 20);
        this.add(xin);
        new1.setBounds(510, 250, 90, 20);
        this.add(new1);

        JLabel xin1=new JLabel("请确认新密码");
        xin1.setBounds(400, 280, 90, 20);
        this.add(xin1);
        new2.setBounds(510,280,90,20);
        this.add(new2);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == sureBtn) {
            if (old.getText().equals(atm.getUsers().get(atm.getA()).getPsd())){
             if (new1.getText().equals(new2.getText())){
                 atm.getUsers().get(atm.getA()).setPsd(new1.getText());
                 atm.switchPanel("login");
                 return;
             }else{
                 JOptionPane.showMessageDialog(this,"两次新密码不一样");

             }


            }else {
                JOptionPane.showMessageDialog(this,"旧密码不正确");
            }

        } else if (e.getSource() == clearBtn) {
            atm.switchPanel("menu");
        }
    }
}
